

/* 
 * File:   main.cpp
 * Author: sean
 *
 * Created on 2012年3月25日, 下午8:56
 */



#include "sqlite_demo.h"

void createdb() {

    int ret;
    sqlite3 *opendb = 0;
    sqlite3_stmt *stmt = 0;
    char *error = 0;
    char *sql = "insert into lrc_demo values('lrcdisplay_col1',:prepare_mark)";//其中prepare_mark为自定义的预处理标记
    int index;
    static void *value = "this is lrcdisplay sqlite3 demo";
    ret = sqlite3_open("lrcdispaly.db", &opendb); //打开数据库，跟打开文本文件一样
    if (ret != SQLITE_OK)
        return;
    ret = sqlite3_exec(opendb, "create table lrc_demo(col1 char(20), col2 BLOB)", 0, 0, &error);
  //  if (ret != SQLITE_OK)
 //       return;
    ret = sqlite3_prepare(opendb, sql, strlen(sql), &stmt, 0);//执行预处理过程
    if (ret != SQLITE_OK)
        return;
    index = sqlite3_bind_parameter_index(stmt, ":prepare_mark");//获取预处理标记
    ret = sqlite3_bind_blob(stmt, index, value, strlen(value), SQLITE_STATIC);
    if (ret != SQLITE_OK)
        return;
    ret = sqlite3_step(stmt);
    if (ret != SQLITE_DONE)
        return;
    sqlite3_close(opendb);
}

char * querydb() {
    int ret;
    sqlite3 *opendb = 0;
    sqlite3_stmt *pstmt = 0;
    const char *error;
    char *sql = "select * from lrc_demo";
    int len;
    int i;
    const char *name;
    const void *value;
    ret = sqlite3_open("lrcdispaly.db", &opendb);
    if (ret != SQLITE_OK)
        return;
    ret = sqlite3_prepare(opendb, sql, strlen(sql), &pstmt, &error);
    if (ret != SQLITE_OK)
        return;
    char *result;
    while (1) {
        ret = sqlite3_step(pstmt);
        if (ret != SQLITE_ROW)
            break;
        name = sqlite3_column_text(pstmt, 0);
        value = sqlite3_column_blob(pstmt, 1);
        len = sqlite3_column_bytes(pstmt, 1);
        printf("name:%s,value:%s,len:%d\n", name, value, len);
      //  result =  strcat(result,strcat(strcat("name",name),strcat("value",value)));
    }
    sqlite3_close(opendb);
    return result;
}
